package com.recyclerview426lkx.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.recyclerview426lkx.R;
import com.recyclerview426lkx.iface.RegisterListener;
import com.recyclerview426lkx.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity implements RegisterListener, View.OnClickListener {

     private EditText et_username,et_userpassword,et_repassword,et_tel;
    private Button btn_register,btn_return;

    private Context context;//上下文
    private SharedPreferences preferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initview();
    }
    private void initview(){

        et_userpassword=(EditText)findViewById(R.id.et_password);
        et_username=(EditText)findViewById(R.id.et_username);
        et_repassword=(EditText)findViewById(R.id.et_rePassword);
        et_tel=(EditText)findViewById(R.id.et_tel);
        btn_register=(Button)findViewById(R.id.btn_register);
        btn_return=(Button)findViewById(R.id.btn_return);
        btn_register.setOnClickListener(this);
        btn_return.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_register:
                String userName=et_username.getText().toString();
                String password1=et_userpassword.getText().toString();
                Log.i("msg",password1);
                String password2=et_repassword.getText().toString();
                Log.i("msg",password2);
                if(!password1.equals((password2))){
                    et_userpassword.setError("密码不一致");
                    return;
                }
                String tel=et_tel.getText().toString();
                RegisterModel mode=new RegisterModel();
                mode.register(userName,password1,tel,this);
                break;
            case R.id.btn_return:
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
    public void onResponse(String regInfo) {
        Log.i("msg",regInfo);
        if(regInfo.contains("1"))
            Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();

        else
            Toast.makeText(this,"注册失败,用户名已存在",Toast.LENGTH_SHORT).show();

        }








    @Override
    public void onFail(String msg) {

    }
}
